Volvo.CareTrack.GooglemapClass = function(params) {

};

proto = Volvo.CareTrack.GooglemapClass.prototype;

proto.resize = function() {
	var h = $('div[data-role="header"]').outerHeight(true);
	var f = $('div[data-role="footer"]').outerHeight(true);
	var w = $(window).height();
	var c = $('div[data-role="content"]');
	var c_h = c.height();
	var c_oh = c.outerHeight(true);
	var c_new = w - h - f - c_oh + c_h;
	var total = h + f + c_oh;
	if (c_h < c.get(0).scrollHeight) {
		c.height(c.get(0).scrollHeight);
	} else {
		c.height(c_new);
	}
};

proto.showMachinePositionOnMap = function(params) {
    $('div[data-role="content"]').height(290);
	myOptions = {
		zoom : 11,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document
			.getElementById("component-MachinePosition-map"), myOptions);
    //alert(params.latitude+'==='+params.longitude);
	myLocation = new google.maps.LatLng(params.latitude, params.longitude);

	image = 'images/truck.png';
	marker = new google.maps.Marker({
		position : myLocation,
		map : map,
		icon : image,
	});

	infowindow = new google.maps.InfoWindow();
	geocoder = new google.maps.Geocoder();
	if (geocoder) {
		geocoder.geocode({'latLng': myLocation}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
			if (results[1]) {
				infowindow.setContent('Address:  '+results[1].formatted_address+'<br/>'+
				                      'Machine#: A30D0074001<br/>Status: Working');
			}
			} else {
			alert("Geocoder failed due to: " + status);
			}
		});
	}
	//infowindow.open(map, marker);
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});

	map.setCenter(myLocation);
};

proto.showMyPositionOnMap = function() {
    $('div[data-role="content"]').height(280);
	var gothenberg = new google.maps.LatLng(57.6969943, 11.9865);
	var myLocation;
	var browserSupportFlag =  new Boolean();
	myOptions = {
		zoom : 13,
		mapTypeId : google.maps.MapTypeId.ROADMAP
	};
	map = new google.maps.Map(document.getElementById("component-MyPosition-map"), myOptions);

	// Try W3C Geolocation (Preferred)
	if (navigator.geolocation) {
		browserSupportFlag = true;
		navigator.geolocation.getCurrentPosition(function(position) {
			myLocation = new google.maps.LatLng(position.coords.latitude,
					position.coords.longitude);
			//alert(position.coords.latitude+'==========='+position.coords.longitude);
			image = 'images/person.png';
			marker = new google.maps.Marker({
				position : myLocation,
				map : map,
				icon : image,
			});
            
			infowindow = new google.maps.InfoWindow();
			geocoder = new google.maps.Geocoder();
			if (geocoder) {
				geocoder.geocode({'latLng': myLocation}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
					if (results[1]) {
						infowindow.setContent(results[1].formatted_address);
					}
					} else {
					alert("Geocoder failed due to: " + status);
					}
				});
			}
			//infowindow.open(map, marker);
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map, marker);
			});

			map.setCenter(myLocation);
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		}, {
			maximumAge : 20000,
			timeout : 20000,
			enableHighAccuracy : true
		});
		// Browser doesn't support Geolocation
	} else {
		browserSupportFlag = false;
		handleNoGeolocation(browserSupportFlag);
	}

	function handleNoGeolocation(errorFlag) {
		if (errorFlag == true) {
			alert("Geolocation service failed.");
		} else {
			alert("Your browser doesn't support geolocation. We've placed you in Gothenberg.");
			myLocation = gothenberg;
		}
		map.setCenter(myLocation);
	}
};

proto.showDirectionsOnMap = function(params) {
  $('div[data-role="content"]').height(600);
  var directionsDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;
  var myLocation;
  navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: 20000, timeout: 20000, enableHighAccuracy: true});

  // Start/Finish icons
  var icons = {
   start: new google.maps.MarkerImage(
    // URL
    'images/person.png',
    // (width,height)
    new google.maps.Size( 32, 32 ),
    // The origin point (x,y)
    new google.maps.Point( 0, 0 ),
    // The anchor point (x,y)
    new google.maps.Point( 22, 32 )
    ),
   end: new google.maps.MarkerImage(
    // URL
    'images/truck.png',
    // (width,height)
    new google.maps.Size( 32, 32 ),
    // The origin point (x,y)
    new google.maps.Point( 0, 0 ),
    // The anchor point (x,y)
    new google.maps.Point( 22, 32 )
   )
  };
 
  function initialize() {
    var myOptions = {
      zoom: 7,
      center: myLocation,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("component-Directions-map"), myOptions);

    directionsDisplay = new google.maps.DirectionsRenderer({
        'map': map,
        'preserveViewport': true,
        'draggable': true,
        'suppressMarkers': true
    });
	directionsDisplay.setPanel(document.getElementById("component-Directions-panel"));
    calcRoute();
  }

  function calcRoute() {
	//myLocation = new google.maps.LatLng(57.6969943, 11.9865);
	//alert(params.latitude+"========"+params.longitude);
    var request = {
        origin: myLocation,
        destination: new google.maps.LatLng(params.latitude, params.longitude),
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
    };

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
	    map.setCenter(response.routes[0].bounds.getCenter());
        directionsDisplay.setDirections(response);
        var leg = response.routes[0].legs[0];
        startMarker(leg.start_location, icons.start, 'My Position' );
        endMarker(leg.end_location, icons.end, 'Machine Position' );
      }
    });
  }
  
  function startMarker(position, icon, title){
    startMarker = new google.maps.Marker({
      position: position,
      map: map,
      icon: icon,
      title: title
    });
	
    startWindow = new google.maps.InfoWindow();
    startWindow.setContent(title);
    google.maps.event.addListener(startMarker, 'click', function() {
       startWindow.open(map, startMarker);
    });
  }

  function endMarker(position, icon, title){
    endMarker = new google.maps.Marker({
      position: position,
      map: map,
      icon: icon,
      title: title
    });
	
    endWindow = new google.maps.InfoWindow();
    endWindow.setContent(title);
    google.maps.event.addListener(endMarker, 'click', function() {
       endWindow.open(map, endMarker);
    });
  }
  // onSuccess Geolocation
  function onSuccess(position) {
	  //alert(position.coords.latitude+'+++'+position.coords.longitude);
	  myLocation = new google.maps.LatLng(position.coords.latitude , position.coords.longitude);
	  initialize();
  }
  // onError Callback receives a PositionError object
  function onError(error) {
      alert('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
  }
}

Volvo.CareTrack.Googlemap = new Volvo.CareTrack.GooglemapClass();